{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6361cd3d",
   "metadata": {},
   "source": [
    "# python网络爬虫应用实战（第20期）第7课书面作业\n",
    "学号：115799\n",
    "\n",
    "**作业内容：**  \n",
    "尝试使用selenium爬取当当上某商品的价格与名称。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1781eabb",
   "metadata": {},
   "source": [
    "**答：**  \n",
    "代码实现如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "696f8ea5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在爬取第 1 页\n",
      " 华为matebook14s保护壳13寸x笔记本pro电脑套D14荣耀magicb、7xf ¥180.00\n",
      " 华为matebook 14保护壳2021锐龙E笔记本电脑保护套13 寸D荣耀magicbook16.1硬15配件软pro ¥47.00\n",
      " 适用MateBook14键盘膜13寸笔记本电脑16.1华为荣耀7xe ¥49.60\n",
      " 华为matebook14s/13笔记本键盘膜xPro保护膜magicbookpro16、7xe ¥57.60\n",
      " 华为matebook14S保护套笔记本电脑13寸保护壳2021款16荣耀magicbook贴膜X透明pro16.1锐龙版 ¥47.00\n",
      " 华为Matebook13/14s键盘膜D笔记本xpro荣耀magicbookv1、7xe ¥67.60\n",
      " 华为matebook14s保护壳13寸x笔记本pro电脑套D14荣耀magicb、7xf ¥180.00\n",
      " 华为matebook笔记本电脑贴纸适用联想小新pro14苹果macbookair13.3寸神州华硕小米惠普戴尔5000卡 ¥42.00\n",
      " 华为matebook14键盘膜13s笔记本D14寸15电脑E荣耀7xe ¥76.00\n",
      " 华为matebook14s键盘保护膜13s适用于荣耀笔记本电脑7xe ¥67.60\n",
      " 华为matebook14s键盘保护膜13s适用于荣耀笔记本电脑7xe ¥67.60\n",
      " 华为matebook14电脑键盘膜mate13荣耀d14笔记本magicbook15、7xe ¥76.00\n",
      " 华为matebook14键盘膜13s笔记本D14寸15电脑E荣耀7xe ¥76.00\n",
      " 华为MateBook13/14扩展坞HDMI拓展坞typec转接头VGA网线接口7xf ¥226.00\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥9888\n",
      " 华为笔记本电脑 MateBook 16 16英寸2.5K全面屏（锐龙标压R7-5800H 16G 512G 高色准多屏协 ¥6688\n",
      " 适用华为MateBook14S键盘膜Mate13寸D14笔记本E电脑XPro荣耀7wy ¥63.60\n",
      " 于华为MateBook键盘膜13s笔记本X14寸荣耀Magicbook V14电脑7xe ¥80.00\n",
      " 适用华为MateBook13/14拓展坞苹果电脑typec扩展器转接头7xf ¥89.60\n",
      " 华为荣耀matebook14笔记本电脑贴纸适用联想thinkpad小新pro16华硕a豆惠普战66 15.6寸戴尔灵越5 ¥42.00\n",
      " 适用华为matebook14/13电脑包女手提可爱10寸平板ipadpro11收纳包 ¥65.00\n",
      " 华为荣耀matebook16卡通笔记本键盘膜适用联想小新air13电脑防尘罩华硕a豆14惠普华硕戴尔小米pro凹凸保护膜 ¥27.00\n",
      " 适用华为MateBook13/14拓展坞苹果电脑typec扩展器转接头7xf ¥89.60\n",
      " 华为笔记本电脑 MateBook 14s 2021款 14.2英寸2.5K触控全面屏（i5-11300H 16GB 51 ¥6488\n",
      " 华为笔记本电脑 MateBook D 14 SE版 14英寸轻薄笔记本（i5-1155G7 8GB 512GSSD 锐炬 ¥3688\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥9988\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥9888\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（R7-4800H 16GB 51 ¥5188\n",
      " 适用于华为MateBook14键盘膜13s笔记本D14电脑X 15荣耀7wy ¥67.60\n",
      " 适用于华为MateBook14键盘膜13s寸15荣耀MagicBook16Pro笔记本7xe ¥65.60\n",
      " 适用于华为MateBook14键盘膜13s笔记本D14电脑X 15荣耀7wy ¥67.60\n",
      " 【支持当当礼卡】华为蓝牙鼠标 AF30无线鼠标适配于华为笔记本电脑MateBook 13 /MateBook 14/ M ¥179.00\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i5 8GB 512G ¥4988\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸轻薄本 2K触控全面屏（AMD 锐龙7 5700U 16GB ¥5488\n",
      " 华为笔记本电脑 MateBook 13s 2021款 13.4英寸 2.5K触控全面屏（i7-11370H 16GB 5 ¥6988\n",
      " 笔记本内胆包适用华为matebook苹果macbookair14寸女小新7xe ¥94.00\n",
      " 华为笔记本电脑 MateBook B5-430 2021款 14.0英寸笔记本电脑（i7-1165G7 16G 512G ¥6988\n",
      " 华为笔记本电脑 MateBook B3-520 2021款 15.6英寸笔记本电脑（i5-1135G7 16G 512G ¥5388\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i7 16GB 512 ¥7688\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i5-1135G7 8GB 512 ¥4988\n",
      " 华为笔记本电脑 MateBook 13s 2021款 13.4英寸 2.5K触控全面屏（i5-11300H 16GB 5 ¥6088\n",
      " 华为笔记本电脑 MateBook X 2021款 13英寸3K触控全面屏轻薄笔记本（i7-1160G7 16G 512G ¥10988\n",
      " 适用huawei华为MateBook E 2022款12.6英寸电脑贴纸机身保护膜二 ¥181.00\n",
      " 华为笔记本电脑 MateBook D 14 2022款 14英寸轻薄笔记本（i5-1155G7 16GB 512GSSD ¥4688\n",
      " 华为HUAWEI MateBook E 12.6英寸OLED全面屏二合一笔记本电脑 轻薄办公本11代酷睿i5 8+256 ¥5999\n",
      " 华为笔记本电脑 MateBook B5-430 2021款 14.0英寸笔记本电脑（i5-1135G7 16G 512G ¥6288\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸轻薄本 2K触控全面屏（AMD 锐龙5 5500U 16GB ¥5288\n",
      " 华为笔记本电脑 MateBook X 2021款 13英寸3K触控全面屏轻薄笔记本（i5-1130G7 16G 512G ¥7588\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i7 16GB 512 ¥6188\n",
      " 华为笔记本电脑 MateBook D 14 2022款 14英寸轻薄笔记本（i5-1155G7 16GB 512GSSD ¥4688\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i7-1165G7 16GB 51 ¥6388\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i5 16GB 512 ¥5588\n",
      " 华为matebook14电脑键盘膜mate13荣耀d14笔记本magicbook15、7xe ¥76.00\n",
      " 华为matebook14s保护套13s硅胶壳软16寸笔记本xpro电脑7xf ¥166.00\n",
      " 华为matebook14s/13笔记本键盘膜xPro保护膜magicbookpro16、7xe ¥57.60\n",
      " 华为matebook14/13S笔记本电脑定制彩绘键盘膜荣耀magicbookV14硅 ¥23.00\n",
      " 华为MateBook14/13E笔记本电脑转换器拓展坞typec转usb3.0接口多功能扩展器转接头插口dock3适用苹 ¥128.00\n",
      " 华为Matebook13/14s键盘膜D笔记本xpro荣耀magicbookv1、7xe ¥67.60\n",
      " 华为MateBook13/14扩展坞HDMI拓展坞typec转接头VGA网线接口7xf ¥226.00\n",
      " 适用MateBook14键盘膜13寸笔记本电脑16.1华为荣耀7xe ¥49.60\n",
      "正在爬取第 2 页\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥11988.00\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥8388.00\n",
      " 华为笔记本电脑 MateBook D 15 2022款 15.6英寸轻薄笔记本（i7-1195G7 16G 512GSS ¥5888.00\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i7-1165G7 16GB 51 ¥6388.00\n",
      " HUAWEI MateBook E 2022款 12.6英寸二合一笔记本（i5-1130G7 16G 512G Win1 ¥6588.00\n",
      " 华为笔记本电脑 MateBook B3-420 2021款 14英寸笔记本电脑（i5-1135G7 16G 512GSS ¥5288.00\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i7-1165G7 16GB 51 ¥7688.00\n",
      " HUAWEI MateBook E 2022款 12.6英寸二合一笔记本（i5-1130G7 8G 256G Win11 ¥5788.00\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i5 16GB 512 ¥5388.00\n",
      " HUAWEI MateBook E 2022款 12.6英寸二合一笔记本（i5-1130G7 8G 256G Win11 ¥5788.00\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸轻薄本 2K触控全面屏（AMD 锐龙7 5700U 16GB ¥5488.00\n",
      " 华为笔记本电脑 MateBook 16 16英寸2.5K全面屏（锐龙标压R7-5800H 16G 512G 高色准多屏协 ¥6688.00\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i5-1155G7 ¥8888\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i5-1135G7 8GB 512 ¥4988\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥8288\n",
      " 华为笔记本电脑 MateBook B3-420 2021款 14英寸笔记本电脑（i7-1165G7 16G 512GSS ¥5988\n",
      " 华为笔记本电脑 MateBook D 15 2022款 15.6英寸轻薄笔记本（i5-1155G7 16G 512GSS ¥4888\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i5 8GB 512G ¥4988\n",
      " 华为笔记本电脑 MateBook 16 16英寸2.5K全面屏（锐龙标压R5-5600H 16G 512G 高色准多屏协 ¥5988\n",
      " 华为HUAWEI MateBook E 12.6英寸OLED全面屏二合一笔记本电脑 轻薄办公本11代酷睿i5 16+51 ¥6999\n",
      " 华为笔记本电脑 MateBook 13s 2021款 13.4英寸 2.5K触控全面屏（i5-11300H 16GB 5 ¥6088\n",
      " 华为HUAWEI MateBook E 12.6英寸OLED全面屏二合一笔记本电脑 轻薄办公本11代酷睿i5 16+51 ¥6999\n",
      " 适用华为2021款matebook14电脑贴纸保护膜13寸笔记本外壳膜pro锐龙版m ¥65.00\n",
      " 华为笔记本电脑 MateBook B7-410 2021款 13.9英寸笔记本电脑（i7-1165G7 16G 512G ¥8988\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i5-1135G7 16GB 51 ¥5388\n",
      " HUAWEI MateBook E 2022款 12.6英寸二合一笔记本（i7-1160G7 16G 512G Win1 ¥7488\n",
      " 笔记本键盘膜适用于华为matebook14华硕vivobook可爱彩绘保护膜小米pro戴尔g3戴尔灵越5000苹果mac ¥27.00\n",
      " 华为笔记本电脑 MateBook D 14 2021款 14英寸轻薄笔记本（i7-1165G7 16GB 512GSSD ¥5688\n",
      " 华为笔记本电脑 MateBook B5-430 2021款 14.0英寸笔记本电脑（i5-1135G7 8G 512GS ¥5888\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸轻薄本 2K触控全面屏（AMD 锐龙5 5500U 16GB ¥5288\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥11988\n",
      " 华为笔记本电脑 MateBook B3-520 2021款 15.6英寸笔记本电脑（i7-1165G7 16G 512G ¥5988\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥11988\n",
      " 华为HUAWEI MateBook E 12.6英寸OLED全面屏二合一笔记本电脑 轻薄办公本11代酷睿i5 8+256 ¥5999\n",
      " 华为笔记本电脑 MateBook 14s 2021款 14.2英寸2.5K触控全面屏（i5-11300H 16GB 51 ¥6488\n",
      " 华为笔记本电脑 MateBook B7-410 2021款 13.9英寸笔记本电脑（i5-1135G7 16G 512G ¥8588\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i5-1135G7 16GB 51 ¥5388\n",
      " 手提电脑包适用华为matebook14寸小米联想小新air13戴尔华硕7xe ¥125.60\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（R7-4800H 16GB 51 ¥5188\n",
      " 华为笔记本电脑 MateBook X 2021款 13英寸3K触控全面屏轻薄笔记本（i5-1130G7 8G 512G ¥7488\n",
      " 华为笔记本电脑 MateBook X 2021款 13英寸3K触控全面屏轻薄笔记本（i5-1130G7 16G 512G ¥7588\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i5 16GB 512 ¥5388\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i5-1135G7 16G ¥7388\n",
      " 华为笔记本电脑 MateBook X 2021款 13英寸3K触控全面屏轻薄笔记本（i7-1160G7 16G 512G ¥10988\n",
      " 华为笔记本电脑 MateBook D 14 2022款 14英寸轻薄笔记本（i7-1195G7 16GB 512GSSD ¥5788\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i7 16GB 512 ¥7688\n",
      " HUAWEI MateBook E 2022款 12.6英寸二合一笔记本（i5-1130G7 16G 512G Win1 ¥6588\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i7 16GB 512 ¥6188\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥8388\n",
      " 华为笔记本电脑 MateBook B3-520 2021款 15.6英寸笔记本电脑（i5-1135G7 8G 512GS ¥4988\n",
      " 华为笔记本电脑 MateBook 16 16英寸2.5K全面屏（锐龙标压R5-5600H 16G 512G 高色准多屏协 ¥5988\n",
      " 华为笔记本电脑 MateBook D 14 2022款 14英寸轻薄笔记本（i7-1195G7 16GB 512GSSD ¥5788\n",
      " 华为笔记本电脑 MateBook 14 2021款 14英寸2K护眼全面屏轻薄笔记本（i7-1165G7 16GB 51 ¥7588.00\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i5-1135G7 16G ¥7388.00\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥7888.00\n",
      " 华为笔记本电脑 MateBook 13s 2021款 13.4英寸 2.5K触控全面屏（i7-11370H 16GB 5 ¥6988.00\n",
      " 华为笔记本电脑 MateBook 13 2021款 13英寸 2K触控轻薄本笔记本电脑（11代酷睿i7 16GB 512 ¥7888.00\n",
      " 手提电脑包适用华为matebook14寸小米联想小新air13戴尔华硕7xe ¥125.60\n",
      " 华为笔记本电脑 MateBook 13s 2021款 13.4英寸 2.5K触控全面屏（i5-11300H 16GB 5 ¥5988.00\n",
      " 华为笔记本电脑 MateBook X Pro 2021款13.9英寸3K高清触控屏商务笔记本（i7-1165G7 16G ¥9988.00\n",
      "正在爬取第 3 页\n",
      " 华为原装无线蓝牙鼠标轻薄便携办公MateBook13/D14/D15/XPro笔记本平板matepad 10.8电脑 ¥94.00\n",
      " 惠普光影精灵7可爱笔记本电脑贴纸适用华为matebook14联想拯救者r7000p15.6寸外壳保护膜小新air小米pr ¥42.00\n",
      " 键盘膜适用于联想Pro13小新Air14定制华为matebook荣耀惠普7xe ¥67.60\n",
      " 华为原装鼠标无线蓝牙第二代轻薄便携笔记本matebook14E13D电脑magicbook平板电脑正品新款 ¥211.00\n",
      " 笔记本内胆包适用联想小米华为matebook14苹果macbook air13.3小 ¥34.00\n",
      " 联想小新Pro14电脑包华为matebook13女笔记本air15点6寸内胆包7xe ¥102.00\n",
      " 联想小新air14笔记本彩绘键盘膜适用华为荣耀matebook16苹果macbookpro13电脑防尘罩惠普星15系列硅 ¥27.00\n",
      " 卡通笔记本电脑贴纸适用戴尔灵越5000华为荣耀matebook14联想拯救者r7000p15.6寸外壳保护膜小新air1 ¥42.00\n",
      " 商务笔记本电脑包女苹果macbook13.3华为matebook14荣耀MagicBook16英寸锐龙版内胆手提pro联 ¥234.00\n",
      " 笔记本键盘膜联想pro13小新air14华为matebook电脑键盘7wy ¥41.60\n",
      " 可爱卡通笔记本键盘膜华为matebook14苹果mac13联想pro16电脑键盘保护膜华硕小米惠普小新air硅胶彩绘防尘 ¥27.00\n",
      " 可爱笔记本电脑贴纸适用联想小新pro14华为荣耀matebook外壳保护拯救者y7000p15.6寸惠普光影精灵7全套定 ¥42.00\n",
      " 卡通笔记本硅胶键盘膜适用联想小新air15华为matebook16惠普星系列彩绘防尘罩联想拯救者r9000p小米pro保 ¥27.00\n",
      " 手提电脑包适用联想小新pro13华为matebook14苹果macbook戴尔小米 ¥34.00\n",
      " 可爱笔记本键盘膜适用华为荣耀matebook16华硕a豆14联想拯救者r7000p15.6寸硅胶彩绘膜小米pro苹果ma ¥27.00\n",
      " 笔记本内胆包适用macbook华为matebook14苹果pro13电脑包air13.3女mac12男15.6英寸d15 ¥267.00\n",
      " 可爱卡通键盘膜适用联想小新air15华为matebook苹果mac13.3寸惠普星系列华硕a豆14笔记本电脑彩绘保护防尘 ¥27.00\n",
      " 电脑贴纸适用联想小新air14华为荣耀matebook16戴尔灵越5000保护膜小米pro惠普星系列华硕a豆笔记本卡通贴 ¥48.00\n",
      " 可爱笔记本键盘膜适用联想小新pro14华为荣耀matebook16卡通保护膜华硕戴尔惠普15.6寸苹果mac13.3电脑 ¥27.00\n",
      " 笔记本电脑包女手提适用华为matebook小米戴尔联想苹果微星神舟战神13/13.3寸14/15.6寸黄猫咪小清新防水面 ¥274.00\n",
      " 笔记本内胆包适用macbook华为matebook14苹果pro13电脑包air13.3女mac12男15.6英寸d15 ¥267.00\n",
      " 卡通可爱笔记本电脑键盘膜适用惠普星14华为matebook15联想小新air小米pro戴尔灵越华硕天选2硅胶彩绘防尘罩 ¥27.00\n",
      " 华为笔记本电脑专用支架适用matebook14可升降13托架平板11架桌面增高型悬空便携matebookxpro散热可升 ¥173.00\n",
      " 联想小新Pro14电脑包华为matebook13女笔记本air15点6寸内胆包7xe ¥102.00\n",
      " 简约笔记本键盘膜彩绘可定制适用华为matebook13联想小新air14小米pro15华硕a豆adolbook戴尔灵越5 ¥27.00\n",
      " 笔记本电脑包手提适用华为matebook14s顺丰13联想小新pro苹果7xe ¥162.00\n",
      " 可爱卡通笔记本键盘膜适用联想小新air14华为matebook16惠普星15系列华硕a豆小米pro苹果mac13.3电脑 ¥27.00\n",
      " 可爱卡通笔记本键盘膜华为matebook13惠普星14联想小新air15.6荣耀magicbookpro16可定制硅胶彩 ¥27.00\n",
      " 笔记本电脑贴纸适用联想小新air14华为荣耀matebook15全套保护膜戴尔灵越华硕a豆惠普战66小米pro苹果mac ¥42.00\n",
      " 卡通电脑包手提女可爱适用华为matebook14寸联想小新air13苹果macbookpro15.6戴尔16英寸ins风 ¥220.00\n",
      " 笔记本键盘膜联想pro13小新air14华为matebook电脑键盘7wy ¥41.60\n",
      " 手提内胆电脑包适用联想小新air13matebook14macbook12小 ¥102.00\n",
      " Typec拓展坞扩展电脑matebook转接头适用于M1苹果MacBookPro、7xf ¥368.00\n",
      " 笔记本键盘彩绘膜联想小新air14戴尔g3惠普星13.3华为matebook14拯救者y7000简约彩绘可定制电脑键盘保 ¥27.00\n",
      " 笔记本键盘膜可爱联想小新air14戴尔g3苹果macbook华为matebook14惠普星14联想拯救者15.6防尘简约 ¥27.00\n",
      " 手提笔记本电脑包适用苹果macbook15.6华为matebook14女联想小新戴尔15pro13.3英寸16华硕air ¥263.00\n",
      " 手提笔记本电脑包14寸女苹果macbookair新款matebook13联想7xe ¥328.00\n",
      " Huawei/华为 原装电脑包Mate Book MateBook包14/15.6英寸 笔记本双肩包商务旅行男女包多功能 ¥108.00\n",
      " 有线鼠标静音无声usb办公商务游戏笔记本台式电脑适用于联想小米戴尔MateBook XPro/14E/13D专用银色超薄 ¥26.00\n",
      " 适用于手提电脑包macbook16寸男pro14英寸保护套air13.3苹果15.6平板笔记本内胆包华为matebook ¥636.00\n",
      " 联想typec扩展坞分线拓展手机usb转接头air配件macbookpro转换器通用电脑matebook14笔记本HUB ¥147.00\n",
      " 联想pro16笔记本电脑贴纸适用小新air14华为matebook全套保护膜小米华硕戴尔灵越5000宏碁苹果mac13外 ¥42.00\n",
      " 卡通笔记本键盘膜适用小米pro14苹果mac13.3华为荣耀matebook16电脑防尘罩惠普联想拯救者y7000p彩绘 ¥27.00\n",
      " 卡通笔记本键盘膜适用联想小新air14惠普星15系列戴尔G3电脑保护膜华硕a豆小米pro华为matebook16凹凸防尘 ¥27.00\n",
      " 笔记本彩绘键盘膜适用华硕a豆14联想小新air15华为荣耀matebook16电脑防尘罩小米pro苹果mac13.3保护 ¥27.00\n",
      " 联想拯救者y7000笔记本电脑贴纸适用小新air14小米pro贴膜华为matebook16宏碁华硕a豆惠普光影精灵全套保 ¥42.00\n",
      " 卡通笔记本电脑贴纸联想拯救者y7000p15.6寸小新air华为matebook14惠普星14华硕a豆戴尔灵越简约ins ¥48.00\n",
      " 卡通笔记本电脑贴纸女联想小新air14拯救者y7000华为matebook13小米pro15华硕a豆惠普战66创意定制防 ¥48.00\n",
      " 【李佳埼推荐】适用于Huawei华为无线鼠标蓝牙静音无声可充电款式联想小米ipad笔记本电脑办公matebook男女生 ¥36.00\n",
      " 时尚彩绘定制笔记本键盘膜适用苹果mac13.3寸联想小新air14红米pro15.6华为matebook16惠普星系列防 ¥27.00\n",
      " 手提笔记本电脑包14寸女苹果macbookair新款matebook13联想7xe ¥328.00\n",
      " 史迪仔笔记本电脑贴纸适用联想小新air14拯救者r7000p15.6寸华为matebook荣耀14寸保护膜华硕a豆苹果m ¥42.00\n",
      " 华为笔记本电脑 MateBook X Pro 2022款 14.2英寸3.1k原色全面屏商务笔记本（i7-1195G7 ¥9888.00\n",
      " 华为笔记本电脑 MateBook B3-420 2021款 14英寸笔记本电脑（i5-1135G7 8G 512GSSD ¥4988.00\n",
      " 可爱笔记本键盘膜适用华为荣耀matebook14联想pro16小新air电脑保护膜华硕戴尔小米苹果mac13彩绘定制防尘 ¥31.00\n",
      " 简约笔记本键盘保护膜华为matebook13寸联想小新air15拯救者Y7000惠普星华硕a豆14小米pro定制硅胶彩绘 ¥27.00\n",
      " 笔记本电脑贴纸适用华为荣耀matebook14联想pro16小新air13华硕a豆卡通贴膜小米戴尔灵越惠普星15可爱保护 ¥42.00\n",
      " Huawei/华为平板笔记本电脑原装内胆包 MateBook X/E保护套轻薄14寸都市商务简约包防刮防泼水 ¥82.00\n",
      " 笔记本电脑包手提适用华为matebook14s顺丰13联想小新pro苹果7xe ¥162.00\n",
      " Typec拓展坞扩展电脑matebook转接头适用于M1苹果MacBookPro、7xf ¥368.00\n",
      "正在爬取第 4 页\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 手提电脑包女 14寸联想小新13可爱内胆包华为荣耀matebook14拯救者可爱华硕苹果笔记本小米卡通好看保护套包 ¥38.00\n",
      " 无线鼠标蓝牙适用于荣耀MagicBook pro15/14/13静音可充电便携双模无声笔记本电脑MateBook商务办公 ¥81.00\n",
      " 联想拯救者r9000p简约笔记本电脑贴纸适用小新air戴尔灵越15pro华硕天选2外壳保护套华为matebook全套保护 ¥42.00\n",
      " 可爱笔记本键盘膜适用惠普战66 15.6苹果mac13.3电脑防尘罩联想小新air14华为荣耀matebook16华硕小 ¥27.00\n",
      " 电脑包手提适用苹果macbookpro笔记本联想小新air14寸女华为matebook13保护套16戴尔15小米内胆包男 ¥212.00\n",
      " 手提电脑包14寸女适用联想苹果macbook air13华为matebook惠普7xe ¥91.20\n",
      " 可爱卡通笔记本键盘膜联想yoga14s小新air15华为matebook电脑键盘保护膜小米惠普戴尔苹果mac硅胶彩绘防尘 ¥27.00\n",
      " 【支持当当礼卡】华为原装12000移动电源/充电宝 双向66W超级快充/优质电芯 适用Mate40Pro/MateBoo ¥329.00\n",
      " 联想拯救者r9000p简约笔记本电脑贴纸适用小新air戴尔灵越15pro华硕天选2外壳保护膜华为matebook14全套 ¥42.00\n",
      " 笔记本电脑贴纸适用惠普星14华硕a豆小米苹果macbookpro13美少女卡通贴膜联想小新air15华为matebook ¥42.00\n",
      " 可爱笔记本电脑键盘膜适用于惠普星14华硕vivobook15华为matebook小米pro戴尔G3彩绘可定制凹凸软硅胶防 ¥27.00\n",
      " 笔记本电脑贴纸适用联想拯救者r9000p16寸y7000p小新air14外壳保护膜华为matebook15惠普小米pro ¥42.00\n",
      " 可爱卡通笔记本电脑贴纸适用联想小新pro14华硕a豆戴尔灵越5000苹果mac13全套保护膜华硕宏碁华为matebook ¥42.00\n",
      " 联想小新air14卡通笔记本电脑贴纸适用拯救者R7000P15.6寸华为荣耀matebook16贴膜华硕惠普小米pro全 ¥42.00\n",
      " 卡通可爱笔记本键盘膜联想pro13小新air14华为matebook15戴尔G3苹果mac系列拯救者r9000可定制彩绘 ¥27.00\n",
      " 适用电脑包苹果macbook pro13寸华为15matebook14电脑air手提15.6男16英寸联想小新微软笔记本 ¥376.00\n",
      " 多啦A梦笔记本贴纸适用联想拯救者y7000p15.6寸华为matebook16小新air14保护膜小米惠普星苹果mac1 ¥42.00\n",
      " 联想拯救者y7000笔记本贴纸简约惠普星15小新pro14华为荣耀matebook防水电脑保护膜苹果mac华硕小米戴尔i ¥48.00\n",
      " 适用于华为MateBookXpro保护壳华为笔记本电脑保护套华为MateBook13/D14保护壳14笔记本配件Magi ¥505.00\n",
      " 电脑包14寸女适用苹果macbook笔记本air华为matebook联想小新pro手提13.3英寸15.6内胆保护套戴尔 ¥291.00\n",
      " 华为原装正品M-Pen lite手写笔M5青春版C5/M6平板电脑matebook e2019细头压感电容触屏笔绘画办公 ¥328.00\n",
      " 笔记本电脑保护膜贴纸联想拯救者r7000p15.6寸华为matebook小新air14宇航员全套贴膜小米pro惠普星系列 ¥42.00\n",
      " 芝麻街笔记本电脑贴纸适用联想小新pro16惠普星系列华硕a豆14保护膜华为matebook15小米戴尔灵越5000全套贴 ¥42.00\n",
      " 多啦A梦笔记本电脑键盘膜适用惠普星14联想小新air13华为matebook15拯救者y7000华硕小米卡通硅胶彩绘防尘 ¥27.00\n",
      " 手提电脑包14寸女适用联想苹果macbook air13华为matebook惠普7xe ¥91.20\n",
      " 笔记本电脑贴纸适用联想拯救者r9000p小新pro16华为matebook14防水保护膜小米惠普星戴尔苹果mac13简约 ¥42.00\n",
      " 可爱卡通笔记本键盘膜适用联想小新air14华为荣耀matebook13电脑防尘罩惠普星系列苹果mac13.3华硕卡通保护 ¥27.00\n",
      " 手提笔记本电脑包适用苹果macbook15.6华为matebook14女联想小新戴尔15pro13.3英寸16华硕air ¥263.00\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from urllib.parse import quote\n",
    "from selenium.webdriver.support.wait import WebDriverWait\n",
    "from selenium.common.exceptions import TimeoutException\n",
    "from selenium.webdriver.support import expected_conditions as EC\n",
    "from selenium.webdriver.common.by import By\n",
    "import re\n",
    "\n",
    "browser = webdriver.Chrome()\n",
    "wait = WebDriverWait(browser,10)\n",
    "keyword = 'matebook'  # 抓取的商品名称\n",
    "pages = 5             # 抓取页数\n",
    "url = 'http://search.dangdang.com/?key='+quote(keyword)+'&act=input'\n",
    "\n",
    "#这个方法用于抓取商品信息,由于商品图片的html类型有两种，故构造了两个正则表达式，其实也可以把data-original设为可选，合并成一个。\n",
    "def get_products():\n",
    "    html = browser.page_source\n",
    "    pattern1 = re.compile('<li ddt-pit=.*?<img src=\"(.*?)\" alt=\"(.*?)\".*?<span class=\"price_n\">(.*?)</span>.*?</li>', re.S)\n",
    "    pattern2 = re.compile('<li ddt-pit=.*?<img data-original=\"(.*?)\".*?alt=\"(.*?)\".*?<span class=\"price_n\">(.*?)</span>.*?</li>',re.S)\n",
    "    items2 = re.findall(pattern2,html)\n",
    "    items1 = re.findall(pattern1,html)\n",
    "    for item in items2:\n",
    "        img,title,price = item\n",
    "        print(title,price)\n",
    "    for item in items1:\n",
    "        img,title,price = item\n",
    "        print(title,price)\n",
    "\n",
    "\n",
    "def index_page(page):\n",
    "    print('正在爬取第',page,'页')\n",
    "    if page == 1:\n",
    "        browser.get(url)\n",
    "        get_products()\n",
    "    else:\n",
    "        try:\n",
    "            #if page>1:\n",
    "            input = wait.until(\n",
    "                EC.presence_of_element_located((By.CSS_SELECTOR,'#t__cp')))\n",
    "            submit = wait.until(\n",
    "                EC.element_to_be_clickable((By.CSS_SELECTOR,'#click_get_page')))\n",
    "            input.clear()\n",
    "            input.send_keys(page)\n",
    "            submit.click()\n",
    "            #text_to_be_present_in_element判断文本是否存在\n",
    "            wait.until(\n",
    "                EC.text_to_be_present_in_element((By.CSS_SELECTOR,'a.current'),str(page)))\n",
    "            wait.until(\n",
    "                EC.presence_of_element_located((By.CSS_SELECTOR,'#search_nature_rg')))#加了这个后就能运行出每一页的内容了\n",
    "            get_products()\n",
    "        except TimeoutException:\n",
    "            index_page(page)#如果超出时间就会重新访问一次\n",
    "\n",
    "\n",
    "\n",
    "for page in range(1,pages):\n",
    "    index_page(page)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d9ccb4e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
